﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Bll.ObjectBlls;
using CSP.Common.Web.Providers;
using CSP.Common.Web;
using CSP.Bll.IObjectBlls;
using CSP.Web.Master;
using CSP.Web.UserControls;

namespace CSP.Web.MyHome.Authentication
{
    public partial class AddCertificate : CSPMyHome
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //指定被选中的导航
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Authentication;
            
            if (!IsPostBack)
            {

            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            CreateAuthentication();
        }

        private void CreateAuthentication()
        {

            CSP.Model.CSP.Authentication authentication = GetAddAuthentication();

            CSPBllFactory factory = CSPBllFactory.GetInstance();
            using (IAuthenticationBll authenticationBll = factory.GetAuthenticationBll())
            {
                try
                {
                    authentication = authenticationBll.Add(authentication, this.TxtUpload.PostedFile);

                }
                catch (Exception ex)
                {
                    LoggerProvider.Error(authentication, ex);
                    this.FailRedirect("提交未失败", "/MyHome/Authentication/List.aspx");
                }
            }
            this.SuccessfullRedirect("已经提交成功", "/MyHome/Authentication/List.aspx");
        }

        /// <summary>
        /// 获取Add所需的Authentication
        /// </summary>
        /// <returns></returns>
        private CSP.Model.CSP.Authentication GetAddAuthentication()
        {
            CSP.Model.CSP.Authentication authentication = new Model.CSP.Authentication
            {
                Username = UserProvider.UserName,
                Name = TxtName.Value,
                Description = TxtDescription.Value,
                Status = 0,
                CertificateType = 2,//其他证书
            };
            return authentication;
        }
    }
}